﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using throneOfGames.Models;
using System.Text;
using System.Security.Cryptography;

namespace throneOfGames.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private IThroneRepository repo = null;

        public HomeController()
        {
            repo = new ThroneRepository();
        }
        public HomeController(IThroneRepository rep)
        {
            repo = rep;
        }

        public ActionResult Index()
        {
            try
            {
                IndexViewModel iVM = new IndexViewModel();
                iVM.games = (from g in repo.AllGames
                            where g.isPublic == true
                            && g.isActive == false
                            select g).ToList();

                iVM.leaderboardData = (from lbData in repo.AllLeaderboardData
                                       orderby lbData.score descending
                                       select lbData).ToList();

                return View(iVM);
            }
            catch
            {
                return View("Error");
            }
        }

        public ActionResult About()
        {
            return View();
        }

        [Authorize]
        public ActionResult MainMenu()
        {
            try
            {
                MainMenuViewModel mm = new MainMenuViewModel();

                mm.games = (from g in repo.AllGames
                            where g.isPublic == true
                            && g.isActive == false
                            select g).ToList();

                mm.boardGames = (from bg in repo.AllBoardgames
                                 select bg).ToList();

                mm.leaderboardData = (from lbData in repo.AllLeaderboardData
                                      orderby lbData.score descending
                                      select lbData).ToList();
                return View(mm);
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
